package com.crayon.account.loader;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.crayon.account.dao.DAOCardBank;
import com.crayon.account.dao.DAOCategory;
import com.crayon.account.dao.DataBaseManager;
import com.crayon.account.data.ENMapKey;
import com.crayon.sharemenu.R;
import com.crayon.util.GDB;
import com.j256.ormlite.dao.Dao;

import java.util.HashMap;

public class LoaderSQLDBInit extends AsyncTaskLoader<HashMap<ENMapKey, Object>> {
	public LoaderSQLDBInit(Context context) {
		super(context);
	}

	@Override
	public HashMap<ENMapKey, Object> loadInBackground() {
		HashMap<ENMapKey, Object> hashMap =new HashMap<ENMapKey, Object>();
		try {
			DataBaseManager baseManager = DataBaseManager.getHelper(getContext());
			Dao<DAOCategory,Integer> daoCate =null;
			try {
				daoCate = baseManager.getDAOCategory();
			} catch (Exception e) {
				// TODO: handle exception
			}

			for(String item :getContext().getResources().getStringArray(R.array.cate_incom)){
				try {
					daoCate.createIfNotExists(new DAOCategory(DAOCategory.INCOM,item));
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			for(String item :getContext().getResources().getStringArray(R.array.cate_outcom)){
				try {
					daoCate.createIfNotExists(new DAOCategory(DAOCategory.OUTCOM,item));
				} catch (Exception e) {
					// TODO: handle exception
				}
			}	
			
		} catch (Exception e) {
			// TODO: handle exception
			GDB.log(getClass(), "err " +e.getMessage());
		}
		//
		try {
			Dao<DAOCardBank,Integer> daoCardBank =DataBaseManager.getHelper(getContext()).getDAOCardBank();
			for(String item :getContext().getResources().getStringArray(R.array.listcards)){
				daoCardBank.createIfNotExists(new DAOCardBank(DAOCardBank.CARD,item));
			}
			for(String item :getContext().getResources().getStringArray(R.array.listbanks)){
				daoCardBank.createIfNotExists(new DAOCardBank(DAOCardBank.BANK,item));
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return hashMap;
	}

	/**
	 * @mail wonsork@gmail.com
	 * @writter WONSORK KIM
	 */
	@Override
	protected void onStartLoading() {
		// TODO Auto-generated method stub
		super.onStartLoading();
		forceLoad();
	}

}


